package com.atguigu.boot2.conf;

import com.atguigu.hello.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.stereotype.Component;

/**
 * @Author yanglin [2533188365@qq.com]
 * @Create 2024-08-07 09:31
 * @Description:
 */
@Component
//public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
public class MyApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    HelloService helloService;

//    @Override
//    public void onApplicationEvent(ApplicationEvent applicationEvent) {
//        //监听应用程序就绪事件并执行方法
//        System.out.println("程序在" + applicationEvent.getTimestamp() + "启动");
//        System.out.println("项目初始化执行方法的方式4，监听应用程序已就绪，执行方法");
//    }


    @Override
    public void onApplicationEvent(ContextRefreshedEvent applicationEvent) {
        //监听应用程序就绪事件并执行方法
        System.out.println("程序在" + applicationEvent.getTimestamp() + "启动");
        System.out.println(applicationEvent.getApplicationContext());
        System.out.println(applicationEvent.getSource());
        System.out.println("项目初始化执行方法的方式4，监听应用程序已就绪，执行方法");
        //
        String s = helloService.sayHello("zhangsan");
        System.out.println(s);
    }


    /**
     * 说明：
     *  ApplicationListener：监听特定的事件，在事件发生时执行
     *  ApplicationEvent：是Spring框架中用于应用程序事件的一个基类，该基类下面有众多的实现
     */

}
